package com.dream.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.catalina.core.AprLifecycleListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.pojo.FriendshipLink;
import com.dream.pojo.Message;
import com.dream.pojo.MessageBoard;
import com.dream.pojo.Mreply;
import com.dream.pojo.UserDream;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;


/*
 * 关于页面的请求控制器
 */
@Controller
@RequestMapping("about")
public class AboutController {


	@Resource(name="Message_Board_Serviceimpl")
	private All_Service_able<MessageBoard> messageBoardServiceimpl;
	
	@Resource(name="Mreply_Serviceimpl")
	private All_Service_able<Mreply> mreply_Serviceimpl;

	/**
	 * 友情链接的业务处理对象
	 */
	@Resource(name="Link_Serviceimpl")
	private All_Service_able <FriendshipLink> link_Serviceimpl;

	protected Object user;

	/**
	 * 跳转到添加友情链接页面的请求
	 */
	@RequestMapping("addlink")
	public String addLink(){
		return "addlink";
	}

	/**
	 * 添加友情链接的请求
	 */
	@RequestMapping("insert")
	public String insertLink(String title,String link_img,String link_http,String domain_name){
		link_Serviceimpl.insert(title, link_img, link_http, domain_name);
		return "forward:/about/read";
	}

	/**
	 * 调转到关于页面页面的请求
	 */
	@RequestMapping("read")
	public String readAbout(Model model){
		//留言墙
		model.addAttribute("messages", messageBoardServiceimpl.list());
		model.addAttribute("list_link",link_Serviceimpl.list());
		return "about";
	}

	/**
	 * 留言
	 */
	@RequestMapping("leaving_a_message")
	@ResponseBody
	public Object leavingAMessage(String context,HttpSession session){
		if(context!=null&&context.trim().length()>0){
			UserDream user =(UserDream)session.getAttribute("user")!=null?(UserDream)session.getAttribute("user"):new UserDream();
			//测试所添加，正式环境，删除
			user.setUid(1);
			user.setQqimg("http://dreamf.cn:80/HeadPortrait/926b4056-b553-47af-b07c-23011e5872e9.png");
			user.setQqname("梦离路");
			MessageBoard message= messageBoardServiceimpl.insert(context,user.getUid().toString());
			DateFormat format= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分"); 
			Message mess=new Message();
			mess.setMid(message.getMid());
			mess.setImg(user.getQqimg());
			mess.setName(user.getQqname());
			mess.setContext(context);
			mess.setTime(format.format(new Date()));
			mess.setState(1);
			Message mess1=new Message();
			mess1.setState(0);
			mess1.setContext("系统繁忙，请稍后再试!");
			return message!=null?mess:mess1;
		}else{
			Message mess =new Message();
			mess.setContext("请登录后再试！");
			mess.setState(-1);
			return mess;
		}
	}
	
	@RequestMapping("reply")
	@ResponseBody
	public Object reply(String context,String mid,HttpSession session){
		if(context!=null&&context.trim().length()>0){
			UserDream user =(UserDream)session.getAttribute("user")!=null?(UserDream)session.getAttribute("user"):new UserDream();
			//测试所添加，正式环境，删除
			user.setUid(1);
			user.setQqimg("http://dreamf.cn:80/HeadPortrait/926b4056-b553-47af-b07c-23011e5872e9.png");
			user.setQqname("梦离路");
			Mreply mrely=mreply_Serviceimpl.insert(context,user.getUid().toString(),mid);
			DateFormat format= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分"); 
			Message mess=new Message();
			mess.setImg(user.getQqimg());
			mess.setName(user.getQqname());
			mess.setContext(context);
			mess.setTime(format.format(new Date()));
			mess.setState(1);
			Message mess1=new Message();
			mess1.setState(0);
			mess1.setContext("系统繁忙，请稍后再试!");
			return mrely!=null?mess:mess1;
		}else{
			Message mess =new Message();
			mess.setContext("请登录后再试！");
			mess.setState(-1);
			return mess;
		}
	}
	
	
}